﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NK.Mvc3.Business;
using NK.Mvc3.Models;
using NK.Mvc3.DBL;
using NK.Mvc3.ViewModels;

namespace NK.Mvc3.Controllers
{ 
    public class StoreInputDetailController : Controller
    {
        private StoreContext db = new StoreContext();

        //
        // GET: /StoreInputDetail/

        public ViewResult Index()
        {
            var storeinputdetails = db.StoreInputDetails.Include(s => s.ProductInfo).Include(s => s.StoreInput);
            return View(storeinputdetails.ToList());
        }

        //
        // GET: /StoreInputDetail/Details/5

        public ViewResult Details(int id)
        {
            StoreInputDetail storeinputdetail = db.StoreInputDetails.Find(id);
            return View(storeinputdetail);
        }

        public ActionResult DetailItem(int id)
        {
            StoreInputDetail storeinputdetail = db.StoreInputDetails.Find(id);
            return PartialView("DetailItem2", storeinputdetail);
        }

        [HttpPost]
        public ActionResult Create(StoreInputDetail storeinputdetail)
        {
            if (ModelState.IsValid)
            {
                var storeIO = new StoreIO(db);
                storeIO.AddStoreInputDetail(storeinputdetail);
                storeIO.Save();
                //storeInput.StoreInputDetails.Add(storeinputdetail);
                //db.SaveChanges();
            }
            return RedirectToAction("Details", "StoreInput", new { id = storeinputdetail.StoreInputID });
        }

        public ActionResult AjaxCreate(int storeInputId)
        {
            ViewBag.StoreInputID = storeInputId;
            return PartialView("AjaxCreate");
        }

        [HttpPost]
        public PartialViewResult AjaxCreate(StoreInputDetail storeinputdetail)
        {
            if (ModelState.IsValid)
            {
                var storeIO = new StoreIO(db);
                storeIO.AddStoreInputDetail(storeinputdetail);
                storeIO.Save();
                //storeInput.StoreInputDetails.Add(storeinputdetail);
                //db.SaveChanges();

                storeinputdetail.ProductInfo = db.ProductInfos.Find(storeinputdetail.ProductInfoID);
                return PartialView("DetailItem3", storeinputdetail);
            }
            return null;
        }

        public PartialViewResult AjaxUpdate(int id)
        {
            StoreInputDetail storeinputdetail = db.StoreInputDetails.Find(id);
            //storeinputdetail.ProductInfo.
            //db.Entry(storeinputdetail).Property(d => d.ProductInfo);
            //storeinputdetail.ProductInfo = db.ProductInfos.Find(storeinputdetail.ProductInfoID);
            return PartialView("AjaxUpdate2",storeinputdetail);
        }

        [HttpPost]
        public PartialViewResult AjaxUpdate(StoreInputDetail storeinputdetail, string btnSubmit)
        {
            if (btnSubmit == "Cancel")
            {
                //storeinputdetail.ProductInfo = db.ProductInfos.Find(storeinputdetail.ProductInfoID);
                //db.Entry(storeinputdetail).Reload();
                storeinputdetail = db.StoreInputDetails.Find(storeinputdetail.ID);
                return PartialView("DetailItem2", storeinputdetail);
            }
            else if (ModelState.IsValid && btnSubmit == "Update")
            {
                var storeIO = new StoreIO(db);
                storeIO.UpdateStoreInputDetail(storeinputdetail);
                storeIO.Save();

                //db.Entry(storeinputdetail).State = EntityState.Modified;
                //db.SaveChanges();


                storeinputdetail.ProductInfo = db.ProductInfos.Find(storeinputdetail.ProductInfoID);
                return PartialView("DetailItem2", storeinputdetail);
            }
            return PartialView("AjaxUpdate2",storeinputdetail);
        }
        
        //
        // GET: /StoreInputDetail/Edit/5
        //[HttpPost]
        public ActionResult Edit(int id)
        {
            StoreInputDetail storeinputdetail = db.StoreInputDetails.Find(id);
            return View(storeinputdetail);
        }

        //
        // POST: /StoreInputDetail/Edit/5

        [HttpPost]
        public ActionResult Edit(StoreInputDetail storeinputdetail)
        {
            if (ModelState.IsValid)
            {
                var storeIO = new StoreIO(db);
                storeIO.UpdateStoreInputDetail(storeinputdetail);
                storeIO.Save();
                //db.Entry(storeinputdetail).State = EntityState.Modified;
                //db.SaveChanges();
            }
            return RedirectToAction("Details", "StoreInput", new { id = storeinputdetail.StoreInputID });
        }

        //
        // GET: /StoreInputDetail/Delete/5
 
        public ActionResult Delete(int id)
        {
            StoreInputDetail storeinputdetail = db.StoreInputDetails.Find(id);
            return View(storeinputdetail);
        }

        //
        // POST: /StoreInputDetail/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            StoreInputDetail storeinputdetail = db.StoreInputDetails.Find(id);
            var storeIO = new StoreIO(db);
            storeIO.RemoveStoreInputDetail(storeinputdetail);
            storeIO.Save();
            //db.StoreInputDetails.Remove(storeinputdetail);
            //db.SaveChanges();
            return RedirectToAction("Index");
        }


        public string AjaxDelete(int id)
        {
            StoreInputDetail storeinputdetail = db.StoreInputDetails.Find(id);

            var storeIO = new StoreIO(db);
            storeIO.RemoveStoreInputDetail(storeinputdetail);
            storeIO.Save();
            //db.StoreInputDetails.Remove(storeinputdetail);
            //db.SaveChanges();
            return string.Empty;
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}